package ModelLayer;
import java.util.ArrayList;

/**
 * Write a description of class OrderCollection here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class OrderCollection
{
    private static ArrayList<Order> orders;
    private static OrderCollection instance;
    private static FileOrder file;
    private static FileDiscount file_discount;
    private static double maximum_discount;
    private static double pickup_discount;
    
    protected OrderCollection()
    {
        file = new FileOrder();
        file_discount = new FileDiscount();
        orders = file.get_data();
        double[] discounts = file_discount.get_discounts();
        maximum_discount = discounts[0];
        pickup_discount = discounts[1];
    }
    
    public static void initialize()
    {
        if(instance == null)
        {
            instance  = new OrderCollection();
        }
    }
    
    public static OrderCollection get_instance()
    {
        return instance;
    }
    
    public static void save_data()
    {
        file.save_changes(orders);
    }
    
    public static String add_order(boolean with_delivery, String delivery_note, String additional_information, String terms_of_delivery, boolean is_complete,  ArrayList<String> ids, ArrayList<String> amounts, double special_discount, double group_discount)
    {
        int max_temp_id = 0;
        int temp_id = 0;
        String id;
        for (Order order : orders)
        {
            temp_id = Integer.parseInt(order.get_id());
            if(max_temp_id < temp_id)
            {
                if(max_temp_id < (temp_id - 1))
                {
                    if(!(is_such_id(id = "" + (max_temp_id + 1))))
                    {
                        break;
                    }
                }
                max_temp_id = temp_id;     
            }
        }
        id = "" + (max_temp_id + 1);           
        orders.add(max_temp_id, new Order(id, with_delivery, delivery_note, additional_information, terms_of_delivery, ids, amounts, is_complete, 0, special_discount));
        find_by_id(id).calculate_total_price(group_discount, maximum_discount, pickup_discount);
        return id;
        
    }
    public static boolean is_such_id(String id)
    {
        for(Order order : orders)
        {
            if(order.get_id().equals(id))
            {
                return true;
            }
        }
        return false;
    }
    
    public static String display_order(String id)
    {
        return find_by_id(id).printable();
    }
    
    public static String display_all_orders()
    {
        String all_data= "";
        for (Order order : orders)
        {
            all_data += "\n\n" + order.printable();
        }
        return all_data;
    }
    
    public static String display_pending_orders()
    {
        String all_data = "";
        for(Order order : orders)
        {
            if(!(order.get_is_complete()))
            {
                all_data += order.open_printable()+ "\n";
            }

        }
        return all_data;
    }
    
    public static Order find_by_id(String input_id)
    {
        for(Order order : orders)
        {
            if(order.get_id().equals(input_id))
            {
                return order;
            }
            
        }
        return null;
    }
    
    public static String display_maximum_discount()
    {
        return "Maximum discount:\t" + maximum_discount + "%";
    }
    
    public static String display_pickup_discount()
    {
        return "Pick up discount:\t" + pickup_discount + "%";
    }
    
    public static void change_maximum_discount(double discount)
    {
        maximum_discount = discount;
    }
    
    public static void change_pickup_discount(double discount)
    {
        pickup_discount = discount;
    }
    
    public static void save_discounts()
    {
        double[] discounts = new double[2];
        discounts[0] = maximum_discount;
        discounts[1] = pickup_discount;
        file_discount.save_discounts(discounts);
    }
    
    public static boolean is_completed(String id)
    {
        return find_by_id(id).get_is_complete();
    }
    
    public static void close_order(String id)
    {
        find_by_id(id).set_is_complete(true);
    }
    
    public static void clear_product(String art_nr)
    {
        for (Order order : orders)
        {
            if(order.get_art_nrs().contains(art_nr))
            {
                order.clear_product(art_nr);
            }
        }
    }
    
    public static double provide_total_cost_of_order(String id)
    {
        return find_by_id(id).get_total_price();
    }
    
}

